package clothes.tools.web;

import clothes.tools.pojo.Purchase;
import clothes.tools.pojo.ResultInfo;
import clothes.tools.service.CommodityService;
import clothes.tools.service.PurchaseService;
import clothes.tools.service.impl.CommodityServiceImpl;
import clothes.tools.service.impl.PurchaseServiceImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

public class PurchaseServlet extends BaseServlet{
    private PurchaseService purchaseService=new PurchaseServiceImpl();
    private CommodityService commodityService=new CommodityServiceImpl();
    /**
     * 添加进货信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType ("text/html;charset=UTF-8");
        String pid = req.getParameter("pid");
        String prime = req.getParameter("prime");
        String enumber = req.getParameter("enumber");
        int enumber1=Integer.parseInt(enumber);
        ResultInfo info = new ResultInfo();
        ObjectMapper mapper = new ObjectMapper();
        if(commodityService.existsCommodityId(pid)) {
            purchaseService.addPurchase(new Purchase(null,pid,new BigDecimal(prime),null,enumber1,null));
            info.setFlag(true);
            info.setErrorMsg("入库成功");
        }
        else {
            info.setFlag(false);
            info.setErrorMsg("商品不存在，入库失败!");
        }
        mapper.writeValue(resp.getOutputStream(), info);
    }

    /**
     * 删除进货信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String eid=req.getParameter("eid");
        int i = Integer.parseInt(eid);
        purchaseService.deletePurchaseById(i);
        resp.getWriter().write("success");
    }

    /**
     * 修改进货信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType ("text/html;charset=UTF-8");
        String pid=req.getParameter("pid");
        String eid=req.getParameter("eid");
        String prime=req.getParameter("prime");
        String enumber=req.getParameter("enumber");
        String totalcost=req.getParameter("totalcost");
        int eid1=Integer.parseInt(eid);
        int enumber1=Integer.parseInt(enumber);
        purchaseService.updatePurchase(new Purchase(eid1,pid,new BigDecimal(prime),null,enumber1,new BigDecimal(totalcost)));
        resp.getWriter().write("success");
    }

    /**
     * 查询所有进货信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr=req.getParameter("page");
        String limitStr=req.getParameter("limit");
//        1、通过CommodityServlet查询全部商品
        List<Purchase> purchases = purchaseService.queryPurchase();
        List<Purchase> purchasesp=purchaseService.queryPurchase(pageStr,limitStr);
        resp.setContentType ("text/html;charset=UTF-8");
        JSONObject jsonObject = new JSONObject ();
        //接收的数据
        JSONArray data = JSONArray.parseArray (JSON.toJSONString (purchasesp));
        jsonObject.put ("data", data);
        jsonObject.put ("code", 0);
        jsonObject.put ("msg", "");
        //获得数据总数
        jsonObject.put ("count", purchases.size ());
        PrintWriter writer = resp.getWriter ();
        writer.print (jsonObject.toJSONString ());
    }
}
